<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<th:block th:include="include :: header('邮件系统')" />
<th:block th:include="include :: list-css" />
<link rel="stylesheet" th:href="@{/js/layuiadmin-1.2.1/lib/extend/wangEditor/wangEditor.min.css}" media="all"/>
<style type="text/css">
	.layui-form-radio{margin-top: 0}
	.getUpload {cursor: pointer;height: 22px;display: block;margin-top: 10px;}
	.shadow{position:relative;box-shadow:0 1px 4px rgba(0,0,0,.3),0 0 20px rgba(0,0,0,.1) inset;background:#fffdfd;border-radius:10px;height: 100vh;overflow: hidden;}
	.shadow::after,.shadow::before{content:"";position:absolute;z-index:-1;bottom:26px;left:10px;width:50%;height:20%;box-shadow:0 15px 10px rgba(0,0,0,.7);transform:rotate(-3deg)}
	.shadow::after{right:10px;left:auto;transform:rotate(3deg)}
    .btn-list {height: auto;text-align: left;padding: 5px 0 5px 40px;margin: 0}
	.person_root {width: auto;margin-top: 3px;margin-left: 3px;border: 1px solid #0095e7;color: #0095e7;border-radius: 3px;display: block;float: left;}
	.person_root span:nth-child(1){width:auto;height:20px;line-height:20px;display:block;float:left;margin-left:5px;margin-right:0;border:0 none;padding-right:5px;color:#0095e7}
	.person_root i{width:20px;display:block;padding:0;float:left;line-height:20px;text-align:center;font-style:normal;text-indent:0;cursor:pointer}
	.toUser_D,.ccUser_D{position: absolute;width: 98%;top: 1px;left:1px;height: auto;}
	.toUser_D p,.ccUser_D p{height: 38px;line-height: 38px;padding-left: 10px;color: #757575;}
	.just_input{width: 0;height:28px;border: none;float: left;margin-left: 4px}
	.input-clear{cursor: pointer;position: absolute;right: -17px;top: 9px;font-size: 15px;}
	.onDelete{cursor:pointer;margin-right: 5px;}
	.onFiles{margin-right: 20px;color: #028ddf;}
</style>
</head>
<body>
<div class="main-content">
	<!--新建邮件-->
	<div class="shadow">
		<form class="layui-form" action="">
			<input type="hidden" id="fileMark" name="fileMark" th:value="${mailContent['id']}">
			<input type="hidden" id="nid" name="nid" th:value="${mailContent['id']}">
			<input type="hidden" id="sendID" name="sendID">
			<input type="hidden" id="sendCaption" name="sendCaption">
			<input type="hidden" id="sendPositionID" name="sendPositionID">
			<input type="hidden" id="sendPositionCaption" name="sendPositionCaption">
			<input type="hidden" id="sendDepartmentID" name="sendDepartmentID">
			<input type="hidden" id="sendDepartmentCaption" name="sendDepartmentCaption">
			
            <div class="layui-form-item btn-list" style="border-radius:10px 10px 0 0 ;">
                <div class="layui-btn layui-btn-danger layui-table-tools" onclick="savepost(true)"><p class="dhicon dh-paper-plane-o" style="color:white;font-size:14px"><span class="fa_span">发 送</span></p></div>
                <div class="layui-btn layui-table-tools importbtn-bg" onclick="savepost(false)"><p class="dhicon dh-cube" style="color:white;font-size:14px"><span class="fa_span">存草稿</span></p></div>
<!--                <div class="layui-btn layui-table-tools importbtn-bg" onclick="getUpload('vHFilesDefault','Mail','',$('#fileFlag'));"><p class="dhicon dh-paperclip" style="color:white;font-size:14px"><span class="fa_span" id="fileFlag">附 件</span></p></div>-->
                <div class="layui-btn layui-table-tools importbtn-bg" >
					<button type="button" class="dhicon dh-paperclip input-clear" style="color:#111111;font-size:14px;display: contents" id="fileSelected">选择上传文件</button>
				</div>
            </div>
            
			<div class="layui-form-item">
				<label class="layui-form-label"><i class="dhicon dh-newspaper-o">&nbsp;收件人：</i></label>
				<div class="layui-input-block">
					<div class="toUser_D"><input class="just_input" type="text" data-from="toUser"></div>
					<input type="text" name="toUserName" id="toUserName" value="" class="layui-input" data-for="toUser" style="color:#fff;" readonly/>
					<i class="layui-icon layui-icon-more getMore" data-for="toUser" ></i>
					<input type="hidden" name="toUser" id="toUser" value="" />
					<i class="dhicon dh-trash-o input-clear" title="清空" onclick="clearUser('toUser')"></i>
				</div>
				<div class="layui-input-block" id="toHide" style="display: none;">
					<input type="text" class="layui-input canntEdit" readonly/>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label"><i class="dhicon dh-newspaper-o">&nbsp;抄送：</i></label>
				<div class="layui-input-block">
					<div class="ccUser_D"><input class="just_input" type="text" data-from="ccUser"></div>
					<input type="text" name="ccUserName" id="ccUserName" value="" class="layui-input" data-for="ccUser" style="color:#fff;" readonly/>
					<i class="layui-icon layui-icon-more getMore" data-for="ccUser" ></i>
					<input type="hidden" name="ccUser" id="ccUser" value="" />
					<i class="dhicon dh-trash-o input-clear" title="清空" onclick="clearUser('ccUser')"></i>
				</div>
				<div class="layui-input-block" id="ccHide" style="display: none;">
					<input type="text" class="layui-input canntEdit" readonly/>
				</div>
			</div>
			<div class="layui-form-item" style="height: auto">
				<label class="layui-form-label">主题：</label>
				<div class="layui-input-block">
					<input type="text" name="mailTitle" id="mailTitle" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item" style="height: auto">
				<label class="layui-form-label">重要邮件：</label>
				<div class="layui-input-block" style="text-align: left">
					<input type="radio" name="important" value="0" title="普通" checked>
					<input type="radio" name="important" value="1" title="急件">
				</div>
			</div>
			<div class="layui-form-item" style="height: auto">
				<label class="layui-form-label">群发邮件：</label>
				<div class="layui-input-block" style="text-align: left">
					<input type="radio" name="reciveKind" value="person" title="个人" lay-filter="reciveKind" checked>
					<input type="radio" name="reciveKind" value="org" title="公司" lay-filter="reciveKind">
					<input type="radio" name="reciveKind" value="dept" title="部门" lay-filter="reciveKind">
				</div>
			</div>

			<div class="layui-form-item" style="height: auto">
				<label class="layui-form-label">附件：</label>
				<div class="layui-input-block" style="text-align: left;padding: 5px 0 0 0;display: flex" id="filesList">
				</div>
			</div>

			<div class="layui-form-item" style="text-align: unset;">
				<label class="layui-form-label">正文：</label>
				<div class="layui-input-block">
					<div id="content">
					</div><br>
					<textarea id="mailContent" name="mailContent"  style="display: none"></textarea>
				</div>
			</div>
		</form>
	</div>
</div>

<script th:src="@{/js/jquery/jquery.min.js}"></script>
<script th:src="@{/js/jquery/jquery.cookie.js}"></script>
<script th:src="@{/js/layuiadmin-1.2.1/layui/layui.js?v=2}"></script>
<script th:src="@{/js/jquery/jquery.blockUI.min.js}"></script>
<script th:src="@{/dhecp/js/dh-common.js?v=53}"></script>
<script th:src="@{/dhecp/js/dh-ui.js?v=37}"></script>
<script th:src="@{/js/layuiadmin-1.2.1/lib/extend/wangEditor/wangEditor.min.js}"></script>
<script th:inline="javascript">
/*<![CDATA[*/
var session = [[${session}]],
	pageType = [[${pageType}]],//页面类型
	mailContent = [[${mailContent}]],//邮件内容
	moduleTable = [[${moduleTable}]];//附件需要
	console.log('session', session);
	console.log('mailContent', mailContent);
/*]]>*/
</script>
<script type="text/javascript">
	$('#sendID').val(session.mandatorUser.userId);
	$('#sendCaption').val(session.mandatorUser.userName);
	// $('#sendPositionID').val(session.mandatorUser.positionGuid);
	$('#sendPositionID').val('UserData');
	$('#sendPositionCaption').val(session.mandatorUser.positionName);
	$('#sendDepartmentID').val(session.mandatorUser.deptId);
	$('#sendDepartmentCaption').val(session.mandatorUser.deptName);

	var $,layer,from,element,util;
	var toUser_obj = [],
		toUserId_obj=[],
		toUserName_obj=[],
		ccUser_obj = [],
		ccUserName_obj=[],
		ccUserId_obj=[];
	switch(pageType){//页面类型
		case 'reply'://回复
			$(".toUser_D").html('<span class="person_root"><span>'+mailContent['sendCaption']+'</span><i class="close" onclick="close_span(this)" oliid="0" data-type="toUser">x</i></span>');
			toUser_obj.push({userId: mailContent['sendID'],userName: mailContent['sendCaption'],positionGuid: mailContent['sendPositionID'],positionName: mailContent['sendPositionCaption'],deptId: mailContent['sendDepartmentID'],deptName: mailContent['sendDepartmentCaption']});
			toUserId_obj.push(mailContent['sendID']+';');
			toUserName_obj.push(mailContent['sendCaption']+';');
			$("#toUserName").val(mailContent['sendCaption']);
			$("#toUser").val(mailContent['sendID']);
			$("#mailTitle").val('回复：'+mailContent['mailTitle']);
			$("#fileFlag").html(mailContent['vHFilesDefault']);
			break;
		case 'replyAll'://回复全部
			break;
		case 'transport'://转发
			$("#mailTitle").val('转发：'+mailContent['mailTitle']);
			$("#fileFlag").html(mailContent['vHFilesDefault']);
			break;
		case 'edit'://编辑
// 			forUser(mailContent['toUserName'],toUserName_obj,'toUserName','toUser','toUser_D');//收件人名赋值
// 			forUser(mailContent['toUser'],toUserId_obj,'toUser');//收件人编号赋值
// 			forUser(mailContent['ccUserName'],ccUserName_obj,'ccUserName','ccUser','ccUser_D');//抄送人名赋值
// 			forUser(mailContent['ccUser'],ccUserId_obj,'ccUser');//抄送人编号赋值

			$("#mailTitle").val(mailContent['mailTitle']);
			$("#content").html(mailContent['mailText']);
			$("#fileFlag").html(mailContent['vHFilesDefault']);
			$("input[name=important][value=1]").attr("checked", mailContent.important ? true : false);
			$("input[name=important][value=0]").attr("checked", mailContent.important ? false : true);
			if(mailContent.reciveKind == "person")$("input[name=reciveKind][value='person']").attr("checked", true);
			if(mailContent.reciveKind == "org")$("input[name=reciveKind][value='org']").attr("checked", true);
			if(mailContent.reciveKind == "dept")$("input[name=reciveKind][value='dept']").attr("checked", true);
			break;
	}
	//正文赋值
	function mailValContent(text,mailTitle=null,reciveMainCaption= null,reciveOtherCaption = null){
		console.log("text",text,mailTitle)
		let content = (mailContent['mailText']?'<DIV>' + mailContent['mailText'] + '</DIV>':'')
			+ '<DIV style="margin-top: 10px">'+text+'</DIV>'
			+ '<DIV style="font-family:宋体;color:#0000ff;font-size:13px;margin-top: 5px"><STRONG>主题: </STRONG><B Style="color:#800000;font-weight:normal">' + (mailTitle?mailTitle:mailContent['mailTitle'])+ '</B></DIV>'
			+ '<DIV style="font-family:宋体;color:#0000ff;font-size:13px;"><STRONG>To: </STRONG><B Style="color:#800000;font-weight:normal">' + (reciveMainCaption?reciveMainCaption:mailContent['reciveMainCaption']) + '</B></DIV>'
			+ (reciveOtherCaption ? ('<DIV style="font-family:宋体;color:#0000ff;font-size:13px;"><STRONG>Cc: </STRONG><B Style="color:#800000;font-weight:normal">' + reciveOtherCaption + '</B></DIV>') : '')
			+ '<DIV style="font-family:宋体;color:#0000ff;font-size:13px;"><STRONG>From: </STRONG><B Style="color:#800000;font-weight:normal">' + session.userId + '/' + session.userName + '/' +( session.mandatorUser?session.mandatorUser.deptName:'') + '/'  + formatTimeMills(new Date())+ '(ECP)</B></DIV>'
			+ '<DIV style="width:100%; border:1px solid #b5c4df;height:0px;margin-bottom:5px;margin-top:5px;display: inline-table;"></DIV>';
		console.log("content",content)
		return content;
	}
	/*
	* 收件人、抄送人赋值
	* dataV  字符串值
	* forObj 目标对象 eg：toUserId_obj
	* forField 目标字段
	* dataType 目标类型
	* forHtml  tag赋值
	* fromBook  true来自通讯录赋值
	* */
	function forUser(dataV,forObj,forField,dataType,forHtml='',fromBook = false){
		var namestr = '';
		if(fromBook){
			if($.inArray(dataV+';', forObj) == '-1'){
				forObj.push(dataV+';');
				$.each(forObj,function (i,v){
					if(v){
						if(forHtml)namestr += '<span class="person_root"><span>'+v.replace(';','')+'</span><i class="close" onclick="close_span(this)" oliid="'+i+'" data-type="'+dataType+'">x</i></span>';
					}
				})
				var reg = new RegExp(",","g");
				$("#"+forField).val(forObj.toString().replace(reg,''));
			}else
				return false;
		}else{
			var str2obj = dataV.split(';');
			$.each(str2obj,function (i,v){
				if(v){
					forObj.push(v+';');
					if(forHtml)namestr += '<span class="person_root"><span>'+v+'</span><i class="close" onclick="close_span(this)" oliid="'+i+'" data-type="'+dataType+'">x</i></span>';
				}
			})
			$("#"+forField).val(dataV);
		}
		if(forField == 'toUserName'){
			namestr += '<input class="just_input" type="text" data-from="toUser">'
		}else if(forField == 'ccUserName'){
			namestr += '<input class="just_input" type="text" data-from="ccUser">'
		}
		if(forHtml)$("."+forHtml).html(namestr);
		if(forField == 'toUserName'){
			$(".toUser_D").parents(".layui-form-item").height($(".toUser_D").height()+5)
			$("#toUserName").height($(".toUser_D").height()+5)
		}else if(forField == 'ccUserName'){
			$(".ccUser_D").parents(".layui-form-item").height($(".ccUser_D").height()+5)
			$("#ccUserName").height($(".ccUser_D").height()+5)
		}
	}

	layui.use(['layer','element','upload','form'], function () {
		$ = layui.jquery,
			layer = layui.layer,
			form = layui.form,
			upload = layui.upload,
			element = layui.element;
		form.on('radio(reciveKind)', function (data) {//监听群发邮件
			switch(data.value){
				case 'person':
					$('#toHide').hide();
					$('#toHide').prev().show();
					$('#ccHide').hide();
					$('#ccHide').prev().show();
					$('#sendPositionID').val('UserData');
					break;
				case 'org':
					$('#toHide input').val('公司群发');
					$('#toHide').prev().hide();
					$('#toHide').show();
					$('#ccHide').prev().hide();
					$('#ccHide').show();
					$('#sendPositionID').val('AllData');
					break;
				case 'dept':
					$('#toHide input').val(session.mandatorUser.deptName);
					$('#toHide').prev().hide();
					$('#toHide').show();
					$('#ccHide').prev().hide();
					$('#ccHide').show();
					$('#sendPositionID').val('DeptData');
					break;
			}
		});
		var uploadData = {'moduleFile':'attach', 'vType':'Mail', 'fileMark':mailContent.id, 'fieldName':'vHFilesDefault','createUser':session.userId}, ext = '';

		if (mailContent.mailFile){
			let mailFiles = mailContent.mailFile.split(";");
			if (mailFiles){
				mailFiles.forEach(item=>{
					if (item){
						let filePath = item.substring(9).replace("///g","\\\\");

						let fileId = filePath.split("{")[1].split("}")[0]
						let contentDiv = "<div class='onFiles' id='"+fileId+"' remak='"+item+"'><i class='dhicon dh-remove onDelete' onclick='deleteFile(\""+fileId+"\")'></i><span>"+filePath.split("]")[1]+"</span></div>"
						$("#filesList").append(contentDiv);
					}
				})
			}
		}

		var uploadInst = upload.render({
			elem: '#fileSelected' //绑定元素
			,multiple: true
			,url: '/system/fileinfo/oaUpload' //上传接口
			,accept: 'file' //普通文件
			,exts: ext
			,data: uploadData
			,before: function(obj){//上传之前
				layer.load(2, {shade: [0.1, '#000']});
			}
			,done: function(res, index, upload){//上传成功
				if(res.code === 0){
					// res.data.filePath
					let filePath = res.data.filePath.substring(9).replaceAll("/", '\\');
					let fileId = filePath.split("{")[1].split("}")[0]
					let contentDiv = "<div class='onFiles' id='"+fileId+"' remak='"+filePath+"'><i class='dhicon dh-remove onDelete' onclick='deleteFile(\""+fileId+"\")'></i><span>"+filePath.split("]")[1]+"</span></div>"
					$("#filesList").append(contentDiv);
				}else{
					$.modal.alert(res.msg,'warning');
				}
				layer.closeAll('loading'); //关闭loading
			}
		});
	});
	<!--js初始化编辑器-->
	var E = window.wangEditor;
	var editor = new E('#content');
	var $mailContent = $('#mailContent');
	editor.config.zIndex = 500;
	editor.config.height = $(window).height() - $('#content').offset().top - 50;
	editor.config.uploadImgServer = '/system/fileinfo/uploadmailimg';
	editor.config.uploadImgParams = {
		moduleFile: 'mailImg',
		vType: 'MailImg',
		fileMark:'mailImg',
		fieldName:'mailImg'
	}
	editor.config.uploadFileName = 'file';
	editor.config.onchange = function (html) {
		// 第二步，监控变化，同步更新到 textarea
		$mailContent.val(html)
	}
	editor.create();
	$mailContent.val(editor.txt.html())

	$(".getMore").click(function(){
		var data_for= $(this).attr('data-for');
		var selectedObject = $("#"+data_for).val();
		var reg1 = new RegExp("\\[","g"),reg2 = new RegExp("]","g");
		selectedObject = selectedObject.replace(reg1,'(');
		selectedObject = selectedObject.replace(reg2,')');
		var title = '选择' + (data_for == 'toUser' ? '收件人' : '抄送人'),
			url = "/system/user/userselect?pagetype=checkbox&by=all&selectedUser="+selectedObject;
		layer.open({
			type: 2//Page层类型
			,area: ['925px', '465px']
			,offset: '10%'
			,title: title
			,shade: 0.6 //遮罩透明度
			,maxmin: true //允许全屏最小化
			,content: url
			,btn: ['确定', '关闭'] //只是为了演示
			,yes: function(index){
				var res = window["layui-layer-iframe" + index].callbackdata();
				if(res.length > 0){
					var reg = new RegExp(",","g");
					var reg2 = new RegExp(";","g");
					if(data_for == 'toUser'){
						toUser_obj = [],toUserName_obj = [],toUserId_obj = [];
						$.each(res,function (i,v) {
							toUserName_obj.push(v['userName'] + '[' + v['userId'] + '];');
							toUserId_obj.push(v['userId'] + ';');
							toUser_obj.push(v);
						})
						var toUserNameStr = toUserName_obj.toString().replace(reg,'');
						var toUserStr = toUserId_obj.toString().replace(reg,'').replace(reg2,',');
						$("#toUserName").val(toUserNameStr.substring(0,toUserNameStr.length-1));
						$("#toUser").val(toUserStr.substring(0,toUserStr.length-1));
						$(".toUser_D").html('<input class="just_input" type="text" data-from="toUser">');
						$.each(toUserName_obj,function (i2,v2) {
							$(".toUser_D .just_input").before('<span class="person_root"><span>'+v2.replace(';','')+'</span><i class="close" onclick="close_span(this)" oliid="'+i2+'" data-type="toUser">x</i></span>');
						})
						$(".toUser_D").parents(".layui-form-item").height($(".toUser_D").height()+5)
						$("#toUserName").height($(".toUser_D").height()+5)
					}else if(data_for == 'ccUser'){
						ccUser_obj = [],ccUserName_obj = [], ccUserId_obj = [];
						$.each(res,function (i,v) {
							ccUserName_obj.push(v['userName'] + '[' + v['userId'] + '];');
							ccUserId_obj.push(v['userId'] + ';');
							ccUser_obj.push(v);
						})
						var ccUserNameStr = ccUserName_obj.toString().replace(reg,'');
						var ccUserStr = ccUserId_obj.toString().replace(reg,'').replace(reg2,',');
						$("#ccUserName").val(ccUserNameStr.substring(0,ccUserNameStr.length-1));
						$("#ccUser").val(ccUserStr.substring(0,ccUserStr.length-1));
						$(".ccUser_D").html('<input class="just_input" type="text" data-from="ccUser">');
						$.each(ccUserName_obj,function (i2,v2) {
							$(".ccUser_D .just_input").before('<span class="person_root"><span>'+v2.replace(';','')+'</span><i class="close" onclick="close_span(this)" oliid="'+i2+'" data-type="ccUser">x</i></span>');
						})

						$(".ccUser_D").parents(".layui-form-item").height($(".ccUser_D").height()+5)
						$("#ccUserName").height($(".ccUser_D").height()+5)
					}
					layer.closeAll();
				}else{
					layer.msg("请选择数据");
				}
			}
			,btn2: function(){
				layer.closeAll();
			}
		});
	})
	//删除tag
	function close_span(obj){
		var reg = new RegExp(",","g");
		var oliid = $(obj).attr('oliid');
			data_type = $(obj).attr('data-type');
		if(data_type == 'toUser'){
			toUserName_obj.splice(oliid, 1);
			toUserId_obj.splice(oliid, 1);
			toUser_obj.splice(oliid, 1);
			var toUserNameStr = toUserName_obj.toString().replace(reg,'');
			var toUserStr = toUserId_obj.toString().replace(reg,'');
			$("#toUserName").val(toUserNameStr.substring(0,toUserNameStr.length-1));
			$("#toUser").val(toUserStr.substring(0,toUserStr.length-1));
			$(".toUser_D").html('<input class="just_input" type="text" data-from="toUser">');
			$.each(toUserName_obj,function (i2,v2) {
				$(".toUser_D .just_input").before('<span class="person_root"><span>'+v2.replace(';','')+'</span><i class="close" onclick="close_span(this)" oliid="'+i2+'" data-type="'+data_type+'">x</i></span>');
			})
			if (toUserName_obj.length > 0){
				$(".toUser_D").parents(".layui-form-item").height($(".toUser_D").height());
				$("#toUserName").height($(".toUser_D").height());
			}
			focus_obj = 'toUser';
		}else if(data_type == 'ccUser'){
			ccUserName_obj.splice(oliid, 1);
			ccUserId_obj.splice(oliid, 1);
			ccUser_obj.splice(oliid, 1);
			var ccUserNameStr = ccUserName_obj.toString().replace(reg,'');
			var ccUserStr = ccUserId_obj.toString().replace(reg,'');
			$("#ccUserName").val(ccUserNameStr.substring(0,ccUserNameStr.length-1));
			$("#ccUser").val(ccUserStr.substring(0,ccUserStr.length-1));
			$(".ccUser_D").html('<input class="just_input" type="text" data-from="ccUser">');
			$.each(ccUserName_obj,function (i2,v2) {
				$(".ccUser_D .just_input").before('<span class="person_root"><span>'+v2.replace(';','')+'</span><i class="close" onclick="close_span(this)" oliid="'+i2+'" data-type="'+data_type+'">x</i></span>');
			})
			if (ccUserName_obj.length > 0){
				$(".ccUser_D").parents(".layui-form-item").height($(".ccUser_D").height());
				$("#ccUserName").height($(".ccUser_D").height());
			}
			focus_obj = 'ccUser';
		}
	}
	// 清空用户
	function clearUser(foruser){
		if(foruser == 'toUser'){
			toUserName_obj = [];
			toUserId_obj = [];
			toUser_obj = [];
			$("#toUserName").val('');
			$("#toUser").val('');
			$(".toUser_D").html('<input class="just_input" type="text" data-from="toUser">');
			$(".toUser_D").parents(".layui-form-item").height("30px");
			$("#toUserName").height("30px");
		}else if(foruser == 'ccUser'){
			ccUserName_obj = [];
			ccUserId_obj = [];
			ccUser_obj = [];
			$("#ccUserName").val('');
			$("#ccUser").val('');
			$(".ccUser_D").html('<input class="just_input" type="text" data-from="ccUser">');
			$(".ccUser_D").parents(".layui-form-item").height("30px")
			$("#ccUserName").height("30px")
		}
	}

	// 删除附件
	function deleteFile(id){
		console.log("删除附件",id)
		$("#"+id).remove()
	}
	function savepost(sendFlag){
		if(!$("#mailTitle").val()){
			$.modal.msgWarning('请填写主题！');
			return false;
		}
		var formData = $(".layui-form").serializeFormToJson();
		var postData = [];
		let fileList = ""

		$('.onFiles').each(function () {
			fileList =fileList+$(this).attr("remak")+";";
		})

		if(sendFlag) {//发送邮件
			if(formData.reciveKind == 'person' && !$("#toUser").val()){
				$.modal.msgWarning('请选择收件人！');
				return false;
			}
//	 		private String mailFile;//附件列表
//	 		private String mailFileSize;//附件大小
			switch(formData.reciveKind){//邮件类型
				case 'person'://个人
					var reciveMainCaption = '',//接收人列表——ID【NAME】
						reciveOtherCaption = '',//抄送人ID列表——ID【NAME】
						reciveIDList = '',//接收人ID列表
						reciveOtherIDList = '';//抄送人ID列表
					$.each(toUser_obj, function(i,v){
						reciveIDList += v.userId + ','; 
						reciveMainCaption += v.userName + '[' + v.userId + '];'
					})
					$.each(ccUser_obj, function(i,v){
						reciveOtherIDList += v.userId + ',';
						reciveOtherCaption += v.userName + '[' + v.userId + '];'
					})
					reciveIDList = reciveIDList.substring(0,reciveIDList.length -1);
					reciveMainCaption = reciveMainCaption.substring(0,reciveMainCaption.length -1);
					reciveOtherIDList = reciveOtherIDList.substring(0,reciveOtherIDList.length -1);
					reciveOtherCaption = reciveOtherCaption.substring(0,reciveOtherCaption.length -1);
					$.each(toUser_obj.concat(ccUser_obj), function(i,v){
						postData.push({
							parentID: mailContent.parentID,
							sendId: formData.sendID,
							sendCaption: formData.sendCaption,
							sendPositionID: formData.sendPositionID,
							sendPositionCaption: formData.sendPositionCaption,
							sendDepartmentID: formData.sendDepartmentID,
							sendDepartmentCaption: formData.sendDepartmentCaption,
							reciveIDList: reciveIDList,
							reciveMainCaption: reciveMainCaption,
							reciveID: v.userId,
							reciveCaption: v.userName,
							reciveDepartmentID: v.deptId,
							reciveDepartmentCaption: v.deptName,
							recivePositionID: v.positionGuid,
							recivePositionCaption: v.positionName,
							reciveOtherIDList: reciveOtherIDList,
							reciveOtherCaption: reciveOtherCaption,
							mailFile : fileList,
							mailDraft: 0,
							sendDeleted: 0,
							sendHided: 0,
							reciveDeleted: 0,
							reciveHided: 0,
							mailSave: 0,
							mailRead: 0,
							mailImportance: (formData.important == 0 ? '普通' : '急件'),
							mailImportInt: formData.important,
							reciveKind: formData.reciveKind,
							mailKind: 0,
							sendAction: (pageType == 'reply' || pageType == 'replyAll' ? '已回复' : (pageType == 'transport' ? '已转发' : '')),
							mailNote: (pageType == 'reply' || pageType == 'replyAll' ? '已回复' : (pageType == 'transport' ? '已转发' : '')),
							mailTitle: formData.mailTitle,
							mailText: encodeURIComponent(mailValContent(formData.mailContent,formData.mailTitle,reciveMainCaption,reciveOtherCaption)),
						});
					})
					break;
				case 'org'://公司
					postData.push({
						parentID: mailContent.parentID,
						sendId: formData.sendID,
						sendCaption: formData.sendCaption,
						sendPositionID: formData.sendPositionID,
						sendPositionCaption: formData.sendPositionCaption,
						sendDepartmentID: formData.sendDepartmentID,
						sendDepartmentCaption: formData.sendDepartmentCaption,
						reciveIDList: null,
						reciveMainCaption: '公司群发',
						reciveID: 'ALL',
						reciveCaption: '公司群发',
						reciveDepartmentID: 'ALL',
						reciveDepartmentCaption: '公司群发',
						recivePositionID: 'ALL',
						recivePositionCaption: '公司群发',
						reciveOtherIDList: null,
						reciveOtherCaption: '',
						mailDraft: 0,
						sendDeleted: 0,
						sendHided: 0,
						reciveDeleted: 0,
						reciveHided: 0,
						mailFile : fileList,
						mailSave: 0,
						mailRead: 1,
						mailImportance: (formData.important == 0 ? '普通' : '急件'),
						mailImportInt: formData.important,
						reciveKind: formData.reciveKind,
						mailKind: 2,
						sendAction: (pageType == 'reply' || pageType == 'replyAll' ? '已回复' : (pageType == 'transport' ? '已转发' : '')),
						mailNote: (pageType == 'reply' || pageType == 'replyAll' ? '已回复' : (pageType == 'transport' ? '已转发' : '')),
						mailTitle: formData.mailTitle,
						mailText: encodeURIComponent(mailValContent(formData.mailContent,formData.mailTitle,公司群发,'')),
					});
					break;
				case 'dept'://部门
					postData.push({
						parentID: mailContent.parentID,
						sendId: formData.sendID,
						sendCaption: formData.sendCaption,
						sendPositionID: formData.sendPositionID,
						sendPositionCaption: formData.sendPositionCaption,
						sendDepartmentID: formData.sendDepartmentID,
						sendDepartmentCaption: formData.sendDepartmentCaption,
						reciveIDList: null,
						reciveMainCaption: session.mandatorUser.deptName,
						reciveID: session.mandatorUser.deptId,
						reciveCaption: session.mandatorUser.deptName,
						reciveDepartmentID: session.mandatorUser.deptPath,
						reciveDepartmentCaption: session.mandatorUser.deptName,
						recivePositionID: session.mandatorUser.deptId,
						recivePositionCaption: session.mandatorUser.deptName,
						reciveOtherIDList: null,
						reciveOtherCaption: '',
						mailDraft: 0,
						sendDeleted: 0,
						sendHided: 0,
						reciveDeleted: 0,
						reciveHided: 0,
						mailFile : fileList,
						mailSave: 0,
						mailRead: 1,
						mailImportance: (formData.important == 0 ? '普通' : '急件'),
						mailImportInt: formData.important,
						reciveKind: formData.reciveKind,
						mailKind: 2,
						sendAction: (pageType == 'reply' || pageType == 'replyAll' ? '已回复' : (pageType == 'transport' ? '已转发' : '')),
						mailNote: (pageType == 'reply' || pageType == 'replyAll' ? '已回复' : (pageType == 'transport' ? '已转发' : '')),
						mailTitle: formData.mailTitle,
						mailText: encodeURIComponent(mailValContent(formData.mailContent,formData.mailTitle,session.mandatorUser.deptName,'')),
					});
					break;
			}
		} else {//保存草稿
			postData.push({
				parentID: mailContent.parentID,
				sendId: formData.sendID,
				sendCaption: formData.sendCaption,
				sendPositionID: formData.sendPositionID,
				sendPositionCaption: formData.sendPositionCaption,
				sendDepartmentID: formData.sendDepartmentID,
				sendDepartmentCaption: formData.sendDepartmentCaption,
				reciveID: 'DraftMail',
				reciveDepartmentID: 'MailReciveFilePath_Edt',
				reciveDepartmentCaption: formData.reciveKind,
				recivePositionID: 'DraftMail',
				recivePositionCaption: 'DraftMail',
				mailFile : fileList,
				mailDraft: 1,
				sendDeleted: 0,
				sendHided: 0,
				reciveDeleted: 0,
				reciveHided: 0,
				mailSave: 0,
				mailRead: 0,
				mailImportance: (formData.important == 0 ? '普通' : '急件'),
				mailImportInt: formData.important,
				reciveKind: formData.reciveKind,
				mailKind: 0,
				mailTitle: formData.mailTitle,
				mailText: encodeURIComponent(mailValContent(formData.mailContent,formData.mailTitle)),
			});
		}
		if(pageType == 'edit'){//编辑邮件（草稿箱）
			if(sendFlag) {//发送邮件
				$.operate.post('/system/oamail/insertMail', {'mailInfoItems': JSON.stringify(postData)}, function(res){
					if(res.code == 0){
						$.post("/system/oamail/deleteMail",{'mailInfoItems':JSON.stringify([{id: mailContent.id}])},function(result){
							$(parent.document).find('.mail-ul .layui-this').click();
						});
					}
				});
			} else {//存草稿
				postData[0].id = mailContent.id;
				$.operate.post('/system/oamail/updateMail', {'mailInfoItems': JSON.stringify(postData)}, function(res){
					if(res.code == 0){
						setTimeout(function(){
							$(parent.document).find('.mail-ul .layui-this').click();
						},1500)
					}
				});
			}
		} else {
			console.log("新增",postData)
			$.operate.post('/system/oamail/insertMail', {'mailInfoItems': JSON.stringify(postData)}, function(res){
				if(res.code == 0){
					setTimeout(function(){
//	 					history.back();
//	 					window.location.href = '/system/oamail/mailList?pageType=inbox';
						$(parent.document).find('.mail-ul .layui-this').click();
					},1500)
				}
			});
		}
	}

	function formatTimeMills(timeMills){
		var date = new Date(timeMills);
		var timeStr = date.getFullYear() + "-";
		if (date.getMonth() < 9)
		{ // 月份从0开始的
			timeStr += '0';
		}
		timeStr += date.getMonth() + 1 + "-";
		timeStr += date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate();
		timeStr += ' ';
		timeStr += date.getHours() < 10 ? ('0' + date.getHours()) : date.getHours();
		timeStr += ':';
		timeStr += date.getMinutes() < 10 ? ('0' + date.getMinutes()) : date.getMinutes();
		timeStr += ':';
		timeStr += date.getSeconds() < 10 ? ('0' + date.getSeconds()) : date.getSeconds();
		return timeStr;
	}


</script>
</body>
</html>